#include "smTextureCubeMap.h"
#include "smError.h" 

namespace ShaderManager
{

smTextureCubeMap::smTextureCubeMap(GLuint texture_unit)
: smTexture(texture_unit)
{

}

void smTextureCubeMap::bind()
{
	glBindTexture(GL_TEXTURE_CUBE_MAP, id);
}

void smTextureCubeMap::unbind()
{
	glBindTexture(GL_TEXTURE_CUBE_MAP,0);
}

void smTextureCubeMap::load(int size, void* data[6], GLuint internal_format, GLuint format, GLuint type, bool linear, bool clamp)
{
	active();
	bind();

	for (int i=0; i<6; i++)
	{
		glTexImage2D(
				GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
				0, //Mipmap : aucun
				internal_format, //GL_RGB16
				size, size,
				0, //Largeur du bord : 0
				format, //GL_RGB,
				type,
				data[i]);
	}

	if(clamp)
	{
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE  );
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE  );
	}
	else
	{
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT );
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT );
	}

	if(linear)
	{
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	}
	else
	{
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	}

	CheckGLError("GL_TEXTURE_CUBE_MAP::load");

	glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
}

}